//
//  DCPersonalInfoPicker.m
//  Demo
//
//  Created by zhaolin on 2023/1/31.
//

#import "DCPersonalInfoAddressPicker.h"
#import "DCAuthPersonalInfoModel.h"


@interface DCPersonalInfoAddressPicker ()<UIPickerViewDelegate,UIPickerViewDataSource>

@property (nonatomic, strong) UIPickerView *datePicker;

@property (nonatomic, strong) NSDictionary *dataSource;

@property (nonatomic, strong) NSArray *privenceArr;

@property (nonatomic, strong) NSArray *cityArr;

@end

@implementation DCPersonalInfoAddressPicker

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if(self) {
        [self configreUI];
    }
    return self;
}

- (void)updateDataArr:(NSDictionary *)data {
    _dataSource = data;
    _privenceArr = [self.dataSource allKeys];
    _cityArr =  _dataSource[[_privenceArr firstObject]];
    [self.datePicker reloadAllComponents];
}

- (void)configreUI {
    //    self.backgroundColor = [[UIColor colorWithString:@"#000000"] colorWithAlphaComponent:0.7];
    UIView*bgView = [UIView new];
    bgView.backgroundColor = [UIColor colorWithString:@"#FFFFFF"];
    [self addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(342);
    }];
    
    UIView *titleView = [UIView new];
    titleView.backgroundColor = [UIColor colorWithString:@"#F0FFCE"];
    [bgView addSubview:titleView];
    [titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(53);
    }];
    
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelBtn.backgroundColor = [UIColor clearColor];
    cancelBtn.titleLabel.font = DCFont_PF_Regular(14);
    [cancelBtn setTitleColor:[UIColor colorWithString:@"#C7C7C7"] forState:UIControlStateNormal];
    [cancelBtn setTitle:@"Cancel" forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(cancelAction:) forControlEvents:UIControlEventTouchUpInside];
    [titleView addSubview:cancelBtn];
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(48);
        make.top.bottom.mas_equalTo(0);
    }];
    
    UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    sureBtn.backgroundColor = [UIColor clearColor];
    sureBtn.titleLabel.font = DCFont_PF_Medium(14);
    [sureBtn setTitleColor:[UIColor colorWithString:@"#1C221B"] forState:UIControlStateNormal];
    [sureBtn setTitle:@"Confirm" forState:UIControlStateNormal];
    [sureBtn addTarget:self action:@selector(sureAction:) forControlEvents:UIControlEventTouchUpInside];
    [titleView addSubview:sureBtn];
    [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-48);
        make.top.bottom.mas_equalTo(0);
    }];
        
    _datePicker = [[UIPickerView alloc] init];
    _datePicker.showsSelectionIndicator= NO;
    _datePicker.backgroundColor = [UIColor clearColor];
    _datePicker.tintColor = [UIColor clearColor];
    _datePicker.delegate = self;
    _datePicker.dataSource = self;
    [bgView addSubview:_datePicker];
    [_datePicker mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo(titleView.mas_bottom);
    }];
}


#pragma mark - picker view
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component == 0) {
        return self.privenceArr.count;
    } else {
        return self.cityArr.count;
    }
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:DCFont_PF_Medium(15)];
        [pickerLabel setTextColor:[UIColor colorWithString:@"#1C221B"]];
        pickerLabel.textAlignment = NSTextAlignmentCenter;
    }
    if (component == 0) {
        pickerLabel.text = [self.privenceArr objectAtIndex:row];
    } else {
        pickerLabel.text = [self.cityArr objectAtIndex:row];
    }

    return pickerLabel;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component  {
    if(component == 0) {
        _cityArr =  _dataSource[[_privenceArr objectAtIndex:row]];
        [pickerView reloadComponent:1];
        [pickerView selectRow:0 inComponent:1 animated:YES];
    }
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 54;
}

- (void)cancelAction:(UIButton *)sender {
    if(self.cancelCallBack) {
        self.cancelCallBack(nil);
    }
}

- (void)sureAction:(UIButton *)sender {
    NSString *privence = [self.privenceArr objectAtIndex:[_datePicker selectedRowInComponent:0]];
    NSString *city = [self.cityArr objectAtIndex:[_datePicker selectedRowInComponent:1]];
    if(self.sureCallBack) {
        self.sureCallBack([NSString stringWithFormat:@"%@   %@",privence,city]);
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
